package org.zoomdev.zoom.web.rendering.impl;

import org.zoomdev.zoom.common.exceptions.ZoomException;
import org.zoomdev.zoom.web.action.ActionContext;
import org.zoomdev.zoom.web.action.MethodActionHandler;
import org.zoomdev.zoom.web.rendering.Rendering;
import org.zoomdev.zoom.web.rendering.TemplateRenderingManager;

import java.io.IOException;

public final class TemplateRendering extends BaseRendering {

    public static final String EXT = HtmlRendering.EXT;

    private final TemplateRenderingManager manager;

    public TemplateRendering(TemplateRenderingManager manager) {
        this.manager = manager;
    }

    @Override
    protected void processThrowable(ActionContext context, Throwable throwable) throws IOException {

    }

    @Override
    protected void processNormal(ActionContext context) throws Exception {
        MethodActionHandler config = context.getAction();
        String ext = config.ext == null ? EXT : config.ext;
        Rendering rendering = manager.getRendering(ext);
        if (rendering == null) {
            throw new ZoomException("找不到后缀" + ext + "的模版支持,请使用TemplateRenderingManager.add('ext',new MyRendering())注册对应的渲染器");
        }
        rendering.render(context);
    }
}
